/**
 * 
 */
package com.goodedeeds.webapp.controller;

import com.goodedeeds.webapp.LoginHandler;
import com.youtills.core.admin.ILookup;
import com.youtills.core.auth.*;
import com.youtills.action.ActionContext;

/**
 * @author ravi
 *
 */
public class BaseAuthAction extends QuiggleAction{

	private IAuthUser authUser;
    private IAuth auth;
    private final IAuthorizationDao authorizationDao;

    protected BaseAuthAction() {
        authorizationDao = AuthDaoFactory.getAuthorizationDao();
    }

    public final IAuthUser getAuthUser(){
        if(authUser == null && getAuth() != null){
            authUser = getAuth().getAuthUser();
        }
        return authUser;
    }

    public final int getAuthUserId(){
        return getAuthUser().getUserId();
    }

    public final IAuth getAuth(){
        if(auth == null){
            auth = (IAuth) getContext().getRequest().getAttribute(LoginHandler.AUTH_KEY);
        }
        return auth;
    }

    protected boolean isAuthorizationAllowed(ILookup permissionType){
        IAuthorization authorization = authorizationDao.getAuthorization(getAuth(), permissionType);
        return authorization != null && authorization.hasAuthorization();
    }

    protected final IAuthorization getAuthorization(){
        return null;
    }

    protected final ActionContext getActionContext() {
        return null;
    }


}
